package com.bosssoft.hr.train.jsp.example.dao.impl;

import com.bosssoft.hr.train.j2se.example.database.JDBCTemplate;
import com.bosssoft.hr.train.jsp.example.dao.UserDao;
import com.bosssoft.hr.train.jsp.example.pojo.User;
import com.bosssoft.hr.train.jsp.example.rommapper.UserRowMapper;

import java.util.List;

/**
 * @description : 数据库操作实现类，包含对于用户的数据库操作 使用JDBC完成功能
 * @author : ChenTong
 * @create : 2020-05-30 10:42
 * @since
 **/
public class UserDaoImpl implements UserDao {
    @Override
    public int insert(User user) {
        String sql = "INSERT INTO user(id,code,name,password) VALUES(?,?,?,?)";
        //获取sql语句所需要的参数
        Object[] params = new Object[]{user.getId(),user.getCode(),user.getName(),user.getPassword()};
        //数据库映射
        //jdbc查询
        return JDBCTemplate.update(sql,params);
    }

    @Override
    public int deleteById(Long id) {
        String sql = "DELETE FROM user WHERE id=? ";
        return JDBCTemplate.update(sql,id);
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE user SET name = ?,code = ?, password = ? WHERE id = ?";
        Object[] params = new Object[]{user.getName(),user.getCode(),user.getPassword(),user.getId()};
        return JDBCTemplate.update(sql,params);
    }



    @Override
    public List<User> listAllUser() {
        String sql = "SELECT * FROM user";
        return JDBCTemplate.query(sql,new UserRowMapper());
    }

    @Override
    public User queryUserByName(String username){
        String sql = "SELECT * FROM user WHERE name=?";
        return JDBCTemplate.query(sql,new UserRowMapper(),username).get(0);
    }


}
